home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / loading / transform_symtab_sort.c < prev    next >
C/C++ Source or Header  |  1991-05-30  |  5KB  |  195 lines

  1. /* File: transform_symtab_sort.c - last edit:
  2.  * lhowe    Wed Nov  1 09:59:31 1989
  3.  * horton       Wed Mar 21 12:13:44 1990  (merged May 30, 1991)
  4.  * Boehm    Th  Mar 21        1991
  5.  *
  6.  * Copyright (C) 1989 by Xerox Corporation. All rights reserved. 
  7.  * Copyright (c) 1989 by Xerox Corporation. All rights reserved. */
  8.  
  9.  
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <sys/param.h>
  13.  
  14. /* This program takes a short ILsymtab.pidXXX file and sorts it by the address
  15.  * which is the first numeric field in a line. The address is first converted 
  16.  * from hexadecimal to base 10. Then qsort is called to do the sort.
  17.  */
  18. /* This handles input format errors very poorly, since there doesn't seem to
  19.  * be an easy way to tell scanf to stop at the end of a line.  Error messages
  20.  * are nearly guaranteed to be wrong.  But they now contain line numbers,
  21.  * which should be correct.  - HB  3/21/91
  22.  */
  23.  
  24. #define MAXENTRIES 1024    /* maximum number of loaded modules we can handle */
  25.  
  26. #define FILENAME    "F:"
  27. #define NODEBUG    "f:"
  28. #define COMMON    "C:"
  29.  
  30. #define    FALSE    0
  31. #define    TRUE    1
  32.  
  33. static int address_compare();
  34. static void selectModules();
  35. static void putModules();
  36.  
  37. /*
  38.  * Other stuff
  39.  */
  40. static long num_entries = 0;    /* actual number of loaded modules in this file# */
  41. static    FILE *file;
  42.  
  43. /* 'struct entry' keeps track of information about each loaded module */
  44. struct entry
  45. {
  46.     unsigned addr_key;
  47.     char field1[4];
  48.     char field2[12];
  49.     char field3[12];
  50.     char field4[12];
  51.     char field5[12];
  52.     char *filename;
  53. }     entries[MAXENTRIES];
  54.  
  55. char *modules[MAXENTRIES];
  56.  
  57.  
  58. sort_main()
  59. {
  60.     file = stdin;
  61.     if (getLoadedModules())
  62.         exit(1);
  63.  
  64.  /******************************************************************
  65.  * Write out the sorted list of loaded modules.
  66.  ******************************************************************/
  67.     file = stdout;
  68.     putModules();
  69. }
  70.  
  71. /***********************************************************************
  72.  * Function: getLoadedModules
  73.  *
  74.  * read in the info from the info file and then we can perform
  75.  * necessary operations on it.  Returns 0 on success
  76.  **********************************************************************/
  77. int getLoadedModules()
  78. {
  79.     int result;
  80.     register int i;
  81.  
  82.  /* Read the names of the modules that have been loaded into pcr */
  83.     i = 0;
  84.     while ((result = fscanf(file, "%3s", entries[i].field1)) != EOF)
  85.     {
  86.     if (result != 1)
  87.     {
  88.         fprintf(stderr, "Cannot read from stdin, wrong format\n");
  89.         return (1);
  90.     }
  91.     entries[i].filename = (char *) malloc(MAXPATHLEN);
  92.     if (entries[i].filename == 0) {
  93.             perror("Sort: getLoadedModules: malloc failed");
  94.             exit(1);
  95.         }
  96.     if ((strcmp(entries[i].field1, FILENAME) == 0) ||
  97.         (strcmp(entries[i].field1, NODEBUG) == 0))
  98.     {
  99.     /* This is a file name line. */
  100.         result = fscanf(file, "%11s %11s %11s %11s %199s\n",
  101.         entries[i].field2, entries[i].field3,
  102.         entries[i].field4, entries[i].field5,
  103.         entries[i].filename);
  104.         if (result != 5 || result == EOF)
  105.         {
  106.         fprintf(stderr, "ERROR: file line %d has wrong format\n",i+1);
  107.         return (1);
  108.         }
  109.     }
  110.     else if (strcmp(entries[i].field1, COMMON) == 0)
  111.     {
  112.     /* This is a common symbol line. */
  113.         result = fscanf(file, "%11s %199s\n", entries[i].field2,
  114.                         entries[i].filename);
  115.         if (result != 2 || result == EOF)
  116.         {
  117.         fprintf(stderr,
  118.                    "ERROR: common symbol line %d has wrong format\n",
  119.                 i+1);
  120.         return (1);
  121.         }
  122.     }
  123.     else
  124.     {
  125.         fprintf(stderr,
  126.             "ERROR: lines must start with either \"F:\" or \"C:\"");
  127.         fprintf(stderr, " Line %d started with \"%s\"\n",
  128.             i+1, entries[i].field1);
  129.         fprintf(stderr, "(may be format error on line %d)\n", i);
  130.         return (1);
  131.     }
  132.         /* convert the address field from a hexadecimal to a long integer */
  133.         entries[i].addr_key = strtol(entries[i].field2, (char **) NULL, 16);
  134.     i++;
  135.     }
  136.     num_entries = i;
  137.  
  138.     qsort(entries, num_entries, sizeof(struct entry), address_compare);
  139.     return (0);    /* success */
  140. }/* end getLoadedModules() */
  141.  
  142. static int address_compare(i, j)
  143. unsigned *i, *j;
  144. {
  145.     return (*i - *j);
  146. }
  147.  
  148.  
  149. /***********************************************************************
  150.  * Function: putModules
  151.  **********************************************************************/
  152. static void putModules()
  153. {
  154.     int i;
  155.  
  156.  
  157.     for (i = 0; i < num_entries; i++)
  158.     {
  159.     if ((strcmp(entries[i].field1, FILENAME) == 0) || (strcmp(entries[i].field1, NODEBUG) == 0))
  160.     {
  161.     /* Write out the file line. */
  162.         /* Keep all the symbols that we have for this file. */
  163.         if (fprintf(file, "%s %s %s %s %s %s\n",
  164.             entries[i].field1, entries[i].field2,
  165.             entries[i].field3, entries[i].field4,
  166.             entries[i].field5,
  167.             entries[i].filename) == EOF)
  168.         {
  169.             fprintf(stderr, "Cannot write to stdout, errno = %d\n", errno);
  170.             return;
  171.         }
  172.  
  173.     }
  174.     else
  175.     {
  176.     /* Write out the common symbol line. */
  177.         if (fprintf(file, "%s %s %s\n",
  178.             entries[i].field1, entries[i].field2,
  179.             entries[i].filename) == EOF)
  180.         {
  181.         fprintf(stderr, "Cannot write to stdout, errno = %d\n", errno);
  182.         return;
  183.         }
  184.     }
  185.     free(entries[i].filename);
  186.     }
  187.  
  188. }
  189.  
  190. /* Revision History
  191. *
  192. * lah    11/1/89        Created from Michael Tao's 10/31/89 version of 
  193. *            selectILsymtab.c.
  194. */
  195.